package cn.unknowpupil.init.controller.system;

import cn.unknowpupil.init.common.enums.BusinessType;
import cn.unknowpupil.init.common.resopnse.ResultResponse;
import cn.unknowpupil.init.components.annotation.Log;
import cn.unknowpupil.init.components.annotation.RepeatSubmit;
import cn.unknowpupil.init.domain.system.SysBackup;
import cn.unknowpupil.init.service.system.ISysBackupService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * @author entfrm
 * @date 2020-03-02 16:33:24
 * @description 备份Controller
 */
@RestController
@RequestMapping("/system/backup")
public class SysBackupController {

    @Autowired
    private ISysBackupService backupService;
    /**
     * 分页
     * @param current
     * @param size
     * @param backup
     * @return
     */
    @PreAuthorize("@ps.hasPerm('system:backup:select')")
    @GetMapping("/list/{current}/{size}")
    public ResultResponse list(@PathVariable("current") int current, @PathVariable("size") int size, SysBackup backup) {
        IPage<SysBackup> backupPage = backupService.page(current,size,backup);
        return ResultResponse.buildSuccess(backupPage);
    }

    /**
     * 获取
     *
     * @param backupId {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：获取权限")
    @GetMapping("get/{backupId}")
    public ResultResponse get(@PathVariable("backupId") Long backupId) {
        SysBackup backup = backupService.info(backupId);
        return ResultResponse.buildSuccess(backup);
    }


    @ApiOperation("系统：备份新增")
    @Log(title = "备份新增", businessType = BusinessType.INSERT)
    @PreAuthorize("@ps.hasPerm('system:backup:create')")
    @RepeatSubmit
    @DeleteMapping("create")
    public ResultResponse create(@RequestBody SysBackup backup) {
        boolean b = backupService.create(backup);
        if(b){
            return ResultResponse.success("备份成功");
        }else {
            return ResultResponse.buildFailure("备份失败");
        }

    }


    @Log(title = "备份还原", businessType = BusinessType.OTHER)
    @PreAuthorize("@ps.hasPerm('system:backup:restore')")
    @GetMapping("/restore/{id}")
    public ResultResponse restore(@PathVariable("id") Long id) {
        return ResultResponse.buildSuccess(backupService.restore(id));
    }



    @Log(title = "备份删除", businessType = BusinessType.DELETE)
    @PreAuthorize("@ps.hasPerm('system:backup:delete')")
    @DeleteMapping("/remove/{ids}")
    public ResultResponse remove(@PathVariable("ids") Long[] ids) {
        //自行增加删除本地文件
        return ResultResponse.buildSuccess(backupService.removeByIds(Arrays.asList(ids)));
    }
}
